package com.hw.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.constants.ConstantCode;
import com.hw.dao.MessageDao;
import com.hw.exceptions.MyBaseException;
import com.hw.pojo.Message;
import com.hw.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName: MessageServiceImpl
 * @Description: 处理业务实现
 * @Author: vie0134
 * @Date: 2020-12-11 09:45
 * @Version: 1.0
 **/
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageDao messageDao;

    @Override
    public void addMessage(Message message) {
        System.out.println(message);
        if (message == null || StringUtils.isEmpty(message.getUsername()) || StringUtils.isEmpty(message.getPassword())) {
            throw new MyBaseException("没有用户信息", ConstantCode.DATA_NOT_TRANSFER);
        }
        Integer id = messageDao.findIdByUsername(message.getUsername());
        if (id != null) {
            throw new MyBaseException("用户名已经存在", ConstantCode.USER_EXIST);
        }
        message.setPassword(DigestUtils.md5DigestAsHex((message.getUsername() + message.getPassword()).getBytes()));

        try {
            messageDao.addMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("网络错误,请检查您的网络", ConstantCode.ERROR);
        }
    }

    @Override
    public void updateMessage(Message message) {
        if (message == null) {
            throw new MyBaseException("没有传递信息", ConstantCode.DATA_NOT_TRANSFER);
        }

        try {
            messageDao.updateMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("网络异常", ConstantCode.ERROR);
        }
    }

    @Override
    public void deleteMessage(Integer id) {
        if (id == null) {
            throw new  MyBaseException("id为传递", ConstantCode.DATA_NOT_TRANSFER);
        }

        try {
            messageDao.deleteMessage(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyBaseException("网络异常", ConstantCode.ERROR);
        }
    }

    @Override
    public Message login(String username, String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new MyBaseException("没有传递用户名或密码", ConstantCode.DATA_NOT_TRANSFER);
        }
        Message message = messageDao.selectMessageByUsername(username);
        if (message == null || !DigestUtils.md5DigestAsHex((username + password).getBytes()).equalsIgnoreCase(message.getPassword()) || message.getStatus() == 0) {
            throw new MyBaseException("用户名或密码不正确", ConstantCode.USERNAME_PASSWORD_ERROR);
        }
        return message;
    }

    @Override
    public Message selectMessageByUsername(String username) {
        if (username == null) {
            throw new MyBaseException("没有传递用户名", ConstantCode.DATA_NOT_TRANSFER);
        }
        Message message = messageDao.selectMessageByUsername(username);
        if (message == null) {
            throw new MyBaseException("用户不存在", ConstantCode.USER_NOT_EXIST);
        }
        return message;
    }

    @Override
    public Message selectMessageById(Integer id) {
        if (id == null) {
            throw new MyBaseException("没有传递id", ConstantCode.DATA_NOT_TRANSFER);
        }
        Message message = messageDao.selectMessageById(id);
        if (message == null) {
            throw new MyBaseException("用户不存在", ConstantCode.DATA_NOT_TRANSFER);
        }
        return message;
    }

    @Override
    public PageInfo<Message> findMessage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Message> messageList = messageDao.findMessage();
        return new PageInfo<Message>(messageList);
    }


}
